package com.demo.test.leak;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.base.tool.log.LogPrint;
import com.demo.Tags;
import com.demo.test.other.ActivityTemp;
import com.fortest.R;

/**
 泄露方式：静态变量引用短生命周期的对象
 泄露原因：退出页面后，Activity还被静态变量引用着，静态变量一旦初始化，要等到进程结束才会结束。

 泄露报告　－－　LeakCanary：
 33726 bytes retained by leaking objects
 Signature: 9c82789656b4788c665abe55098be7d31a56546
 ┬───
 │ GC Root: System class
 │
 ├─ com.demo.test.other.ActivityTemp class
 │    Leaking: NO (a class is never leaking)
 │    ↓ static ActivityTemp.activity
 │                          ~~~~~~~~
 ╰→ com.demo.test.leak.ActivityLeakB_StaticVariable instance
 ​     Leaking: YES (ObjectWatcher was watching this because com.demo.test.leak.ActivityLeakB_StaticVariable received Activity#onDestroy() callback and Activity#mDestroyed is true)
 ​     key = 41400bae-0d1e-488a-9828-04a21dda51db
 ​     watchDurationMillis = 5985
 ​     retainedDurationMillis = 982

 **/
public class ActivityLeakB_StaticVariable1 extends Activity {


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_leak_common);
        TextView textView = findViewById(R.id.helper_text);
        textView.setText(" 泄露方式：静态变量引用短生命周期的对象\n" +
                " 泄露原因：退出页面后，Activity还被静态变量引用着，静态变量一旦初始化，要等到进程结束才会结束。");
        ActivityTemp.activity = this;
    }


    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        LogPrint.e(Tags.gc, this.getLocalClassName() + "  finalize");
    }
}
